<template>
  <div class="bottom-menu">
    <CheckButton class="select-all" v-model="isSelectAll" @checkBtnClick="checkBtnClick"></CheckButton>
    <span>全选</span>
    <span class="total-price">合计: ¥{{totalPrice}}</span>
    <span class="buy-product" @click="calcClick">去计算({{countNums.length}})</span>
  </div>
</template>

<script>
  import CheckButton from './CheckButton'
  import {addOrder,Orders} from 'network/shopcar.js'

	export default {
		name: "BottomBar",
    components: {
		  CheckButton
    },
    computed: {
		  totalPrice() {
        const carList = this.$store.state.carList;
        return carList.filter(item => {
          return item.checked
        }).reduce((preValue, item) => {
          return preValue + item.realPrice * item.shopCount
        }, 0).toFixed(2)
      },
      isSelectAll() { 
        return this.$store.state.carList.find(item => item.checked) === undefined?false:true
      },
      countNums() {
        const carList = this.$store.state.carList;
        return carList.filter(item => item.checked);
      },
    },
    methods: {
      checkBtnClick: function () {
        // 1.判断是否有未选中的按钮
        let isSelectAll = this.$store.state.carList.find(item => !item.checked);

        // 2.有未选中的内容, 则全部选中
        if (isSelectAll) {
          this.$store.state.carList.forEach(item => {
            item.checked = true;
          });
        } else {
          this.$store.state.carList.forEach(item => {
            item.checked = false;
          });
        }
      },
      calcClick() {
        if(!this.isSelectAll){
          this.$toast.show("请选择您要购买的商品",2000);
        }else {
          // 因为对象操作不方便，所以只能将数据以字符串形式传回
          // let array = new Orders(this.countNums,this.totalPrice,this.countNums.length,this.$store.state.user.username); 
          let array = [];
          array.push(this.$store.state.user[0].username);
          array.push(this.totalPrice);
          array.push(this.countNums.length);
          this.countNums.forEach((item) => {
            array.push(item.iid);
            array.push(item.realPrice*item.shopCount);
            array.push(item.shopCount);
          })
          // let array = [1,2,3]
    
          let arr = array.join('|');
          this._addOrder(arr);
        }
      },
      _addOrder(obj) {
        addOrder(obj).then((res) => {
          if(res.code === 1) {
            this.$toast.show("加入订单成功",2000);
            this.$emit("createQrCode",res.data);
          }
        })
      },
    }
	}
</script>

<style scoped>
  .bottom-menu {
    width: 100%;
    height: 44px;
    background-color: #eee;
    position: fixed;
    bottom: 50px;
    left: 0;
    box-shadow: 0 -2px 3px rgba(0, 0, 0, .2);
    font-size: 14px;
    color: #888;
    line-height: 44px;
    padding-left: 35px;
    box-sizing: border-box;
  }

  .bottom-menu .select-all {
    position: absolute;
    line-height: 0;
    left: 12px;
    top: 13px;
  }

  .bottom-menu .total-price {
    margin-left: 15px;
    font-size: 16px;
    color: #666;
  }

  .bottom-menu .buy-product {
    background-color: orangered;
    color: #fff;
    width: 100px;
    height: 44px;
    text-align: center;
    line-height: 44px;
    float: right;
  }
</style>
